//
//  PxReaderView.h
//  Express
//
//  Created by 泰然财富 on 15/7/28.
//  Copyright (c) 2015年 泰然财富. All rights reserved.
//

#import "PxReaderView.h"
#import "LLSmallUtil.h"

static NSTimeInterval kQrLineanimateDuration = 0.02;

@implementation PxReaderView {
    
    UIImageView *qrLine;
    CGFloat qrLineY;
    UILabel *qrMenu;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    if (!qrLine) {
        [self initQRLine];
        
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:kQrLineanimateDuration target:self selector:@selector(show) userInfo:nil repeats:YES];
        [timer fire];
    }
    if (!qrMenu) {
        [self initQrMenu];
    }
}

- (void)initQRLine {
    CGFloat tap = 1;
    qrLine  = [[UIImageView alloc] initWithFrame:CGRectMake(self.scanRect.origin.x + tap, self.scanRect.origin.y, self.scanRect.size.width - tap*2, 1)];
//    qrLine.backgroundColor = [UIColor colorWithHTMLStr:@"2c75c7"];
    qrLine.image = [UIImage imageNamed:@"Images/ss-line"];
    qrLine.contentMode = UIViewContentModeScaleAspectFill;
    
    [self addSubview:qrLine];
    qrLineY = qrLine.frame.origin.y;
}

- (void)initQrMenu {
    qrMenu = [[UILabel alloc] initWithFrame:CGRectMake(0, self.scanRect.origin.y+self.scanRect.size.height, self.bounds.size.width, 50)];
    qrMenu.font = [UIFont systemFontOfSize:12];
    qrMenu.textColor = [UIColor whiteColor];
    qrMenu.textAlignment = NSTextAlignmentCenter;
    qrMenu.text = @"将条码放入框内，即可自动扫描";
    [self addSubview:qrMenu];
}

- (void)show {
    
    [UIView animateWithDuration:kQrLineanimateDuration animations:^{
        
        CGRect rect = qrLine.frame;
        rect.origin.y = qrLineY;
        qrLine.frame = rect;
        
    } completion:^(BOOL finished) {
        
        CGFloat maxBorder = self.scanRect.origin.y + self.scanRect.size.height - 38;
        if (qrLineY > maxBorder) {
            
            qrLineY = self.scanRect.origin.y;
        }
        qrLineY++;
    }];
}

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [self addScreenFillRect:ctx rect:rect];
    
    [self addCenterClearRect:ctx rect:self.scanRect];

    [self addWhiteRect:ctx rect:self.scanRect];
    
    [self addCornerLineWithContext:ctx rect:self.scanRect];
}

- (void)addScreenFillRect:(CGContextRef)ctx rect:(CGRect)rect {
    
    CGContextSetRGBFillColor(ctx, 40 / 255.0,40 / 255.0,40 / 255.0,0.5);
    CGContextFillRect(ctx, rect);   //draw the transparent layer
}

- (void)addCenterClearRect :(CGContextRef)ctx rect:(CGRect)rect {
    
    CGContextClearRect(ctx, rect);  //clear the center rect  of the layer
}

- (void)addWhiteRect:(CGContextRef)ctx rect:(CGRect)rect {
    
    CGContextStrokeRect(ctx, rect);
    CGContextSetRGBStrokeColor(ctx, 1, 1, 1, 1);
    CGContextSetLineWidth(ctx, 0.8);
    CGContextAddRect(ctx, rect);
    CGContextStrokePath(ctx);
}

- (void)addCornerLineWithContext:(CGContextRef)ctx rect:(CGRect)rect{
    
    //画四个边角
    CGContextSetLineWidth(ctx, 2);
    CGContextSetRGBStrokeColor(ctx, 255 /255.0, 78/255.0, 8/255.0, 1);//绿色
    
    //左上角
    CGPoint poinsTopLeftA[] = {
        CGPointMake(rect.origin.x+0.7, rect.origin.y),
        CGPointMake(rect.origin.x+0.7 , rect.origin.y + 15)
    };
    
    CGPoint poinsTopLeftB[] = {CGPointMake(rect.origin.x, rect.origin.y +0.7),CGPointMake(rect.origin.x + 15, rect.origin.y+0.7)};
    [self addLine:poinsTopLeftA pointB:poinsTopLeftB ctx:ctx];
    
    //左下角
    CGPoint poinsBottomLeftA[] = {CGPointMake(rect.origin.x+ 0.7, rect.origin.y + rect.size.height - 15),CGPointMake(rect.origin.x +0.7,rect.origin.y + rect.size.height)};
    CGPoint poinsBottomLeftB[] = {CGPointMake(rect.origin.x , rect.origin.y + rect.size.height - 0.7) ,CGPointMake(rect.origin.x+0.7 +15, rect.origin.y + rect.size.height - 0.7)};
    [self addLine:poinsBottomLeftA pointB:poinsBottomLeftB ctx:ctx];
    
    //右上角
    CGPoint poinsTopRightA[] = {CGPointMake(rect.origin.x+ rect.size.width - 15, rect.origin.y+0.7),CGPointMake(rect.origin.x + rect.size.width,rect.origin.y +0.7 )};
    CGPoint poinsTopRightB[] = {CGPointMake(rect.origin.x+ rect.size.width-0.7, rect.origin.y),CGPointMake(rect.origin.x + rect.size.width-0.7,rect.origin.y + 15 +0.7 )};
    [self addLine:poinsTopRightA pointB:poinsTopRightB ctx:ctx];
    
    CGPoint poinsBottomRightA[] = {CGPointMake(rect.origin.x+ rect.size.width -0.7 , rect.origin.y+rect.size.height+ -15),CGPointMake(rect.origin.x-0.7 + rect.size.width,rect.origin.y +rect.size.height )};
    CGPoint poinsBottomRightB[] = {CGPointMake(rect.origin.x+ rect.size.width - 15 , rect.origin.y + rect.size.height-0.7),CGPointMake(rect.origin.x + rect.size.width,rect.origin.y + rect.size.height - 0.7 )};
    [self addLine:poinsBottomRightA pointB:poinsBottomRightB ctx:ctx];
    CGContextStrokePath(ctx);
}

- (void)addLine:(CGPoint[])pointA pointB:(CGPoint[])pointB ctx:(CGContextRef)ctx {
    CGContextAddLines(ctx, pointA, 2);
    CGContextAddLines(ctx, pointB, 2);
}


@end